<?php
/**
 * 验证后台用户身份令牌中间件。
 *
 * @author    Fairyin <fairyin@126.com>
 * @copyright © 2016 www.imcn.vip
 * @version   v1.0
 */

namespace App\Http\Middleware;

use Cookie;
use Closure;
use Request;
use Response;
use Redirect;
use App\Http\Model as MM;
use Illuminate\Support\Facades\Auth;

class ShadowMiddleware
{
    public function handle($request, Closure $next)
    {
        $s_token = isset($_COOKIE['u']) ? $_COOKIE['u'] : Request::get('u');
        //token 不存在或不符合规则
        if ($s_token == null || $s_token == '' || strlen($s_token) < 80) {
            return Redirect::to('/shadow/login');
        }
        $b_flag = MM\User::checkToken($s_token);
        //token 验证失败，清除 cookie 并退出
        if (!$b_flag) {
            return Redirect::to('/shadow/login');
        }
        
        //继续向下执行
        return $next($request);
    }
}
